﻿Imports System.Data.OleDb

Public Class ProfileEdit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'to refresh/update the database first rather than pageload
        If Not IsPostBack Then





            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
            Dim SqlString As String = " SELECT * FROM UserProfile WHERE Username =@f25"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

            'Finding the user from the database

            cmd.CommandType = CommandType.Text
            'find out the user name.
            cmd.Parameters.AddWithValue("@f25", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()

                'found the user and just databinding the textboxes with user detail.
                'if not null for when there is no data value within the systerm.

                If Not IsDBNull(dr("Title")) Then ddltitle.SelectedValue = dr("Title")
                If Not IsDBNull(dr("FirstName")) Then tbxFname.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tbxLname.Text = dr("LastName")
                If Not IsDBNull(dr("Email")) Then tbxEmail.Text = dr("Email")
                If Not IsDBNull(dr("Gender")) Then ddlgender.SelectedValue = dr("Gender")
                If Not IsDBNull(dr("DOB")) Then tbxDOB.Text = dr("DOB")
                If Not IsDBNull(dr("AboutMe")) Then tbAboutMe.Text = dr("AboutMe")
                If Not IsDBNull(dr("FavouriteColour")) Then ddlFColour.SelectedValue = dr("FavouriteColour")


            End If
        End If
    End Sub

    Protected Sub btn_saveProfile_Click(sender As Object, e As EventArgs) Handles btn_saveProfile.Click



        Dim newFileName As String = ""
        Dim SQL As String

        If FUPicture.HasFile Then

            'Change/ Update record in the database profile table
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocation = Request.MapPath("Pictures") & "/" & newFileName
            FUPicture.SaveAs(fileLocation)
        End If

        If FUPicture.HasFile Then 'if there is a picture to be added than add it
            SQL = "UPDATE UserProfile SET Email=@f1,Title=@f2,FirstName=@f3,LastName=@f4,DOB=@f5,AboutMe=@f6,RegistrationDate=@f7,FavouriteColour=@f8,Gender=@f9,UserImage=@f10 WHERE Username=@f11 "

        Else
            ' if not  than remove it from the update
            SQL = "UPDATE UserProfile SET Email=@f1,Title=@f2,FirstName=@f3,LastName=@f4,DOB=@f5,AboutMe=@f6,RegistrationDate=@f7,FavouriteColour=@f8,Gender=@f9 WHERE Username=@f11 "

        End If



        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        conn.Open()

        cmd.Parameters.AddWithValue("@f1", tbxEmail.Text)
        cmd.Parameters.AddWithValue("@f2", ddltitle.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", tbxFname.Text)
        cmd.Parameters.AddWithValue("@f4", tbxLname.Text)
        cmd.Parameters.AddWithValue("@f5", Now.Date.ToString("dd/MM/yyyy"))
        cmd.Parameters.AddWithValue("@f6", tbAboutMe.Text)
        cmd.Parameters.AddWithValue("@f7", Now.Date.ToString("dd/MM/yyyy"))
        cmd.Parameters.AddWithValue("@f8", ddlFColour.SelectedValue)
        cmd.Parameters.AddWithValue("@f9", ddlgender.SelectedValue)
        ' if the file upload FU has a picture to upload than do it
        If FUPicture.HasFile Then cmd.Parameters.AddWithValue("@f10", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@11", User.Identity.Name)

        cmd.ExecuteNonQuery()

        'remove everthing from memory
        cmd.Dispose()
        conn.Close()
        conn.Dispose()

        ' take the user to their profile page
        Response.Redirect("Profile.aspx?Username=" & User.Identity.Name)


    End Sub
End Class